const path = require("path");
const resolve = (dir) => path.resolve(__dirname, dir);
const HTMLWEBPACKPLUGIN = require("html-webpack-plugin");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
module.exports = {
  mode: "development",
  entry: "./src/main.ts",
  output: {
    path: resolve("dist"),
    filename: "bundle.js",
    // 这个选项是告诉webpack 不要使用箭头函数
    environment: {
      arrowFunction: false,
    },
  },
  resolve: {
    extensions: [".ts", ".js", ".cjs"],
  },
  module: {
    rules: [
      {
        test: /\.ts$/g,
        use: [
          // 配置babel
          {
            loader: "babel-loader",
            // 设置babel
            options: {
              presets: [
                [
                  // 设置预定义的环境
                  "@babel/preset-env",
                  {
                    // 指定兼容浏览器
                    targets: {
                      chrome: "58",
                      ie: "8",
                    },
                    // 指定corejs的版本
                    corejs: "3",
                    // 使用corejs的方式
                    useBuiltIns: "usage", // 表示按需加载
                  },
                ],
              ],
            },
          },
          "ts-loader",
        ],
        // 排除的文件
        exclude: /node_modules/,
      },
    ],
  },
  plugins: [
    new HTMLWEBPACKPLUGIN({
      title: "自定义title",
      template: "./public/index.html",
    }),
    new CleanWebpackPlugin(),
  ],
};
